package com.bruce.testxlog;

import android.content.Context;
import android.os.Environment;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @author:
 * @project: WanAndroidPractice
 * @package: com.bruce.ktglobalscopelx
 * @description:
 * @date: 2020/4/11
 * @time: 18:27
 */
public class FileUtils {
    /**
     * app根目录路径
     */
    public static final String DIR_APP = getAppDir();
    /**
     * 临时目录路径
     */
    public static final String DIR_TEMP = getTempDir();

    /**
     * GIF路径
     */
//    public static final String DIR_GIF = getCarGifDir();

    /**
     * Log路径
     */
    public static final String DIR_LOG = getLogDir();
    /**
     * 图片缓存目录路径
     */
    public static final String DIR_IMAGE_CACHE = getImgCheDir();
    /**
     * 临时图片路径
     */
    public static final String PATH_TEMP_IMAGE = DIR_TEMP + File.separator + "temp.jpg";
    /**
     * apk路径
     */
    public static final String PATH_APK = DIR_TEMP + File.separator;
    /**
     * GIF 路径
     */
//    public static final String PATH_GIF = DIR_GIF + File.separator;

    /**
     * GIF 路径
     */
//    public static String getPathGifFile(String id) {
//        return DIR_GIF + File.separator + "car" + id + ".zip";
//    }

    /**
     * apk包名
     */
    public static final String PACKAGE_NAME = XLogApp.getContext().getPackageName();
    /**
     * 获取app根目录路径
     */
    private static String getAppDir() {
//        String appDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +
//                PACKAGE_NAME;
        String appDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/xLog测试/";
        File dir = new File(appDir);
        if (!dir.exists()) {
            dir.mkdir();
        }
        return appDir;
    }

    /**
     * 获取临时目录
     */
    private static String getTempDir() {
        String tempDir = DIR_APP + File.separator + "temp";
        File dir = new File(tempDir);
        if (!dir.exists()) {
            dir.mkdir();
        }
        return tempDir;
    }

    /**
     * 获取图片缓存目录
     */
    private static String getImgCheDir() {
        String tempDir = DIR_APP + File.separator + "imgCache";
        File dir = new File(tempDir);
        if (!dir.exists()) {
            dir.mkdir();
        }
        return tempDir;
    }

    /**
     * 获取Gif目录
     */
//    private static String getCarGifDir() {
//        String tempDir = DIR_APP + File.separator + "CarGif";
//        File dir = new File(tempDir);
//        if (!dir.exists()) {
//            dir.mkdir();
//        }
//        return tempDir;
//    }

    /**
     * 获取Log目录
     */
    private static String getLogDir() {
        String tempDir = DIR_APP + File.separator + "Log";
//        String tempDir = DIR_APP + File.separator + "Log";
        File dir = new File(tempDir);
        if (!dir.exists()) {
            dir.mkdir();
        }
        return tempDir;
    }

    /**
     * 获取cache路径
     *
     * @param context
     * @return
     */
    public static String getDiskCachePath(Context context) {
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            return context.getExternalCacheDir().getPath();
        } else {
            return context.getCacheDir().getPath();
        }
    }

    /**
     * 清空临时目录下的文件
     */
    public static void clearTempDir() {
        File file = new File(FileUtils.DIR_TEMP);
        if (file == null) {
            return;
        }
        File[] files = file.listFiles();
        if (files == null || files.length < 1) {
            return;
        }
        for (int i = 0; i < files.length; i++) {
            files[i].delete();
        }
    }


    /**
     * 使用系统当前日期加以调整作为照片的名称
     *
     * @return
     */
    public static String getPhotoFileName() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "'IMG'_yyyyMMdd_HHmmssSSS", Locale.getDefault());
        return dateFormat.format(date) + "_" + (int) (Math.random() * 100) + ".jpg";
    }

    public static void Unzip(String zipFile, String targetDir, String id) {
        int BUFFER = 4096; //这里缓冲区我们使用4KB，
        String strEntry; //保存每个zip的条目名称


        try {
            BufferedOutputStream dest = null; //缓冲输出流
            FileInputStream fis = new FileInputStream(zipFile);
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
            ZipEntry entry; //每个zip条目的实例

            while ((entry = zis.getNextEntry()) != null) {

                try {
                    int count;
                    byte data[] = new byte[BUFFER];
                    strEntry = entry.getName();

                    File dir = new File(targetDir + File.separator + id);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }

                    File entryFile = new File(dir.getAbsolutePath() + File.separator + strEntry);
                    File entryDir = new File(entryFile.getParent());

                    if (!entryDir.exists()) {
                        entryDir.mkdirs();
                    }

                    FileOutputStream fos = new FileOutputStream(entryFile);
                    dest = new BufferedOutputStream(fos, BUFFER);
                    while ((count = zis.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, count);
                    }
                    dest.flush();
                    dest.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            zis.close();
        } catch (Exception cwj) {
            cwj.printStackTrace();
        }
    }
}
